<?php

/**
 * This is the model class for table "{{news}}".
 *
 * The followings are the available columns in table '{{news}}':
 * @property integer $id
 * @property string $title
 * @property string $body
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $user_id
 * @property integer $image_id
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Comments $comment
 */
class News extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{news}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				array('title, body', 'required'),
				array('create_time,image_id, update_time, user_id,status', 'numerical', 'integerOnly'=>true),
				array('title', 'length', 'max'=>255),
				// The following rule is used by search().
				// Please remove those attributes that should not be searched.
				array('id, title, body, create_time,image_id, update_time, user_id, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'user' => array(self::BELONGS_TO, 'User', 'user_id'),
				'image' => array(self::BELONGS_TO, 'Files', 'image_id'),
				'comments' => array(self::HAS_MANY, 'Comments', 'item_id','condition'=>"status=1 AND item_type='news'"),
				'commentCount' => array(self::STAT, 'Comments', 'item_id', 'condition'=>"status=1 AND item_type='news'"),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'title' => 'Tiêu đề',
				'body' => 'Nội dung',
				'create_time' => 'Ngày tạo',
				'update_time' => 'Ngày cập nhật',
				'user_id' => 'Người đăng',
				'image_id'=> 'Ảnh',
				'status' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

	public function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->create_time = time();
				
			if(!Yii::app()->user->isGuest)
			{
				$this->user_id = Yii::app()->user->id;
			}else{
				throw new CHttpException(404,'Bạn không có quyền đăng tin!');
			}

		}else{
			$this->update_time = time();
		}
		
		return parent::beforeSave();
	}

	/**
	 * @param $comment Comments
	 * @return mixed
	 */
	public function addComment($comment)
	{
		if(Yii::app()->params['commentNeedApproval'])
			$comment->status=2;
		else
			$comment->status=1;
		$comment->item_type = 'news';
		$comment->item_id=(int)$this->id;
		return $comment->save();
	}
	
	public function getUrl()
	{
		return Yii::app()->createUrl('news/view', array(
				'id'=>$this->id,
				'title'=>$this->title,
		));
	}
}